<?php

namespace App\Http\Service;

use App\Http\Util\Result;
use App\Model\Community;
use Illuminate\Support\Facades\DB;

trait ServiceCommunity{

    /**
     * czs
     * @return mixed
     * 社群列表
     */
    public static function getList($keyWord,$limit){
        $model = DB::table('communities as a');
        if (!empty($keyWord)) {
            $model->where("a.name", "like", '%' . $keyWord . '%');
        }
        $list = $model->leftJoin('scenery_regions as b', 'b.id', '=', 'a.region_id')
            ->orderBy('a.id','desc')
            ->select('a.*','b.name as bname')
            ->paginate($limit);
        return Result::success()->setData($list);
    }

    /**
     * czs
     *删除记录
     */
    public static function deleteLine($id) {
        $res = Community::where("id", $id)->delete();
        return Result::success()->setData($res);
    }

    /**
     *获取单个id记录
     * czs
     */
    public static function getOne($id) {
        $res = Community::where("id", $id)->first();
        return Result::success()->setData($res);
    }

    /**
     *更新记录
     * czs
     */
    public static function updateData($input, $id) {
        $res = DB::table('communities')->where("id", $id)->update($input);
        return Result::success()->setData($res);
    }

    /**
     * 禁用、启用
     * czs
     */
    public static function able($id,$able) {
        DB::table('communities')->where("id", $id)->update(['status' => $able]);
        if ($able==2) {
            return Result::success()->setMsg("在售")->setData(2);
        } else {
            return Result::success()->setMsg("禁用")->setData(1);
        }
    }

    /**
     * web调取社群
     * czs
     */
    public static function ajaxCommunity($rid) {
        $arr = [
            0=>39,1=>34,2=>37,3=>36,4=>31,
            5=>33,6=>32,7=>28,8=>12,9=>26,
            10=>23,11=>22,12=>29,13=>40,14=>11,
            15=>7,16=>21,17=>27,18=>25,19=>35,
            20=>15,21=>14,22=>2,23=>13,24=>24,
            25=>17,26=>18,27=>19,28=>16,29=>37,
            30=>30,31=>20,
        ];
        $rid = $arr[$rid];
        $res = DB::table('communities')->where('region_id',$rid)->get();
        return Result::success()->setData($res);
    }
}


